var form;
var laydate;

var currentCategoryId;

$(document).ready(function () {
    layui.use(['form', 'laydate'], function () {
        var form = layui.form;
        form.render();

        initDater();
        getCharge();

        // 监听Select
        form.on("select(selectFilter)", function(data) {
            $("input[name='householdId']").val(data.value);
        });

        // 监听Select
        form.on("select(selectChargeFilter2)", function(data) {
            $("#categoryIdEdit").val(data.value);
        });


    });
});

function initDater() {
    laydate.render({
        elem: '#chargeYearMonthEdit'
        ,type: 'month'
    });
}

function getCharge() {

    var id = $('#id').val();

    ajax(
        "/admin/charge/getCharge", "POST",
        { chargeId: id }
        ,true, true, false
    ).done(function (returnData) {

        $("input[name='chargeId']").val(returnData.chargeId);
        $("input[name='chargeYearMonth']").val(returnData.chargeYearMonth);
        $("input[name='chargeMoney']").val(returnData.chargeMoney);
        $("input[name='householdId']").val(returnData.householdId);
        $("input[name='householdName']").val(returnData.householdName);
        $("input[name='householdNo']").val(returnData.householdNo);
        $("input[name='categoryId']").val(returnData.categoryId);
        currentCategoryId = returnData.categoryId;
        var select = $("#chargeHouseholdName");

        if (returnData.householdName != null) {
            select.empty();
            select.append("<option value=''>搜索收费</option>");
            select.append("<option value='" + returnData.householdId + "' selected>" + returnData.householdName + "</option>");
        }

        form.render();
        // 监听输入框
        reloadSelectListener();

        getCategory();

    });

}

function editCharge() {

    var formJSON = $("#charge-submit-form").serializeObject();

    // 编辑收费
    ajax(
        "/admin/charge/editCharge", "POST",
        formJSON, true, true, true
    ).done(function () {
        loadingPage("/admin/charge/charge");
    });

}

function getCategory() {

    ajax(
        "/admin/charge/getCategory", "POST",
        null,true, true, false
    ).done(function (returnData) {

        var select = $("#categoryNameEdit");
        select.empty();
        select.append("<option value=''>点击选择缴费类别</option>");

        for (var i = 0; i < returnData.length; i ++) {
            if (currentCategoryId === returnData[i].categoryId) {
                select.append("<option value='" + returnData[i].categoryId + "' selected>" + returnData[i].categoryName + "</option>");
            }
            else {
                select.append("<option value='" + returnData[i].categoryId + "'>" + returnData[i].categoryName + "</option>");
            }
        }

        form.render('select');
        reloadSelectListener();
    });

}

function onSelectChange() {
    var householdName = $("#houseHouseholdDiv>div>div>input").val();
    ajax(
        "/admin/charge/getHousehold", "POST",
        { householdName: householdName }
        ,false, true, false
    ).done(function (returnData) {
        if (returnData.length > 0) {

            var select = $("#chargeHouseholdName");
            select.empty();
            select.append("<option value=''>搜索住户</option>");

            for (var i = 0; i < returnData.length; i ++) {
                select.append("<option value='" + returnData[i].householdId + "'>" + returnData[i].householdName + "(" + returnData[i].householdNo + ")</option>");
            }

            form.render('select');
            reloadSelectListener();
        }


    });
}

function reloadSelectListener() {

    // 监听输入框
    $("#houseHouseholdDiv>div>div>input").bind("input propertychange", function() {
        onSelectChange();
    });

}

